package com.ruoyi.web.controller.workflow;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.CarPackageAudit;
import com.ruoyi.workflow.service.ICarPackageAuditService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 套餐信息Controller
 * 
 * @author xiaodu
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/workflow/carPackageAudit")
public class CarPackageAuditController extends BaseController
{
    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    /**
     * 查询套餐信息列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:carPackageAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }


    /**
     * 根据id查询流程进度
     * @param id 流程实例id
     */
    @GetMapping("processImg/{id}")
    @PreAuthorize("@ss.hasPermi('workflow:carPackageAudit:processImg')")
    public void readProcessImg(@PathVariable Long id,HttpServletResponse resp) throws IOException {
        InputStream in = carPackageAuditService.readProcessImg(id);
        ServletOutputStream out = resp.getOutputStream();
        IOUtils.copy(in,out);
    }

    /**
     * 撤销审核流程
     * @param id
     * @return
     */
    @PostMapping("cancel")
    @PreAuthorize("@ss.hasPermi('workflow:carPackageAudit:cancel')")
    public AjaxResult cancelProcess(Long id,String reason){
        carPackageAuditService.cancelProcess(id,reason);
        return AjaxResult.success("撤销审核成功");
    }

    /**
     * 审批历史
     * @param instanceId
     * @return
     */
    @GetMapping("history/{instanceId}")
    @PreAuthorize("@ss.hasPermi('workflow:carPackageAudit:history')")
    public TableDataInfo historyProcess(@PathVariable String instanceId){
        List<Map<String, Object>> mapList = carPackageAuditService.historyProcess(instanceId);
        return getDataTable(mapList);
    }
}
